// 书写打包规则
const gulp = require('gulp')
const cssmin = require('gulp-cssmin')
const autoprefixer = require('gulp-autoprefixer')
const uglify = require('gulp-uglify')
const babel = require('gulp-babel')
const sass = require('gulp-sass')(require('sass'))
const htmlmin = require('gulp-htmlmin')
const fileInclude = require('gulp-file-include')
// const imagemin = require('gulp-imagemin')
const del = require('del')
const server = require('gulp-webserver')
const cssHandler = () => {
  return gulp
  .src('./src/css/*.css')
  .pipe(autoprefixer())
  .pipe(cssmin())
  .pipe(gulp.dest('./dist/css/'))
}
const jsHandler = () => {
  return gulp
  .src('./src/js/*.js')
  .pipe(babel({presets: ['@babel/preset-env']}))
  .pipe(uglify())
  .pipe(gulp.dest('./dist/js/'))
}
/* const sassHandler = () => {
  return gulp
  .src('./src/sass/*.scss')
  .pipe(sass())
  .pipe(autoprefixer())
  .pipe(cssmin())
  .pipe(gulp.dest('./dist/sass'))
} */
const htmlHandler = () => {
  return gulp
  .src('./src/views/*.html')
 /*  .pipe(fileInclude({
    basepath:'./src/componets/',
    prefix:'@',
  })) */
  .pipe(htmlmin({
    collapseWhitespace: true, // 删除空白
      collapseBooleanAttributes: true, // 去除布尔类型属性
      removeAttributeQuotes: true, // 移除双引号
      removeComments: true, // 移除注释
      removeEmptyAttributes: true, // 移除空属性
      removeScriptTypeAttributes: true, // 移除 script 标签的默认 type 属性
      removeStyleLinkTypeAttributes: true, // 移除 style 和 link 标签的 type 属性
      minifyCSS: true, // 压缩内嵌式 css 代码
      minifyJS: true, // 压缩内嵌式 js 代码
  }))
  .pipe(gulp.dest('./dist/views'))
}
const imgHandler = () => {
  return gulp
  .src('./src/img/**.*' )
  // .pipe(imagemin())
  .pipe(gulp.dest('./dist/img'))
}
const videoHandler = () => {
  return gulp
  .src('./src/videos/**.*')
  .pipe(gulp.dest('./dist/videos/'))
}
const audioHandler = () => {
  return gulp
  .src('./src/audios/**.*')
  .pipe(gulp.dest('./dist/audios/'))
}
const fontHandler = () => {
  return gulp
  .src('./src/font/**.*')
  .pipe(gulp.dest('./dist/font'))
}
const delHandler = () => {
  return del(['./dist/'])
}
/* const serverHandler = () => {
  return gulp 
  .src('./dist')
  .pipe(server({
    host: 'www.ys.com',
    post: 9999,
    open:'./views/index.html',
    livereload: true,
      proxies: [
      {
        source: '/dt',
        target: 'https://www.duitang.com/napi/blog/list/by_filter_id/', 
      }
    ] 
   }))
} */
/* const watchHandler = () => {
  gulp.watch('./src/views/*.html',htmlHandler)
  gulp.watch('./src/css/*.css',cssHandler)
  gulp.watch('./src/js/*.js',jsHandler)
  gulp.watch('./src/sass/**.*',sassHandler)
} */
const _default = gulp.series(
  delHandler,
  // gulp.parallel(cssHandler,jsHandler, sassHandler,htmlHandler,imgHandler,videoHandler,audioHandler,fontHandler,),
  gulp.parallel(cssHandler,jsHandler,htmlHandler,imgHandler),
  // serverHandler,
  // watchHandler
)
  

module.exports = {
  cssHandler,
  jsHandler,
  htmlHandler,
  imgHandler,
  // videoHandler,
  // audioHandler,
  // fontHandler,
  delHandler,
  default: _default
}

